package com.cnpc.framework.base.service.impl;

import com.cnpc.framework.base.entity.Role;
import com.cnpc.framework.base.pojo.Result;
import com.cnpc.framework.base.service.RoleService;
import com.cnpc.framework.constant.RedisConstant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.*;

@Service("roleService")
public class RoleServiceImpl extends BaseServiceImpl implements RoleService {



    @Override
    public Result delete(String id) {

        String hql = "from UserRole where roleId='" + id + "'";
        if (this.find(hql).isEmpty()) {
            Role role = this.get(Role.class, id);
            this.delete(role);
            return new Result(true);
        }
        return new Result(false, "该角色已经绑定用户，请先解绑用户");
    }

    @Override
    public Set<String> getRoleCodeSet(String userId) {
        /*String sql= PropertiesUtil.getValue("shiro.sql.roles");
        Map<String,Object> params=new HashMap<String,Object>();
        params.put("param",userId);
        List<Map<String,Object>> list=super.findMapBySql(sql,params);
        Set<String> retSet=new HashSet<String>();
        for (Map map : list) {
            retSet.add(map.get("code").toString());
        }
        return retSet;*/
        List<Role> roles=getRoleList(userId);
        Set<String> roleSet=new HashSet<>();
        for (Role role : roles) {
            roleSet.add(role.getCode());
        }
        return roleSet;

    }

    @Override
    public List<Role> getRoleList(String userId){
        String key= RedisConstant.ROLE_PRE + userId;
        //List<Role> roles=redisDao.getList(key,Role.class);
        List<Role> roles= null;
        System.out.println(roles==null?roles:roles.size());
        if(roles==null || roles.size() ==0 ){
            String sql="select r.* from tbl_user_role ur left join tbl_role r on ur.roleId=r.id " +
                    "left join tbl_user u on ur.userId=u.id where u.id=:userid order by r.sort";
            Map<String,Object> params=new HashMap<>();
            params.put("userid",userId);
            roles=this.findBySql(sql,params,Role.class);
            //redisDao.add(key,roles);
            return roles;
        }
        return roles;
    }

    @Override
    public Result getList(String name, Integer pageNum, Integer pageSize) throws Exception {
        String sql = "select * from tbl_role where";
        String cntsql = "select count(*) from tbl_role where";

        String orderBy = " order by  sort asc,create_date_time desc ";
        StringBuilder whereClause = new StringBuilder();
        Map<String, Object> params = new HashMap<>();

        if (StringUtils.isNotEmpty(name)) {
            whereClause.append("and  (name like :name ) ").append(" ");
            params.put("name", "%"+name+"%");
        }

        if (whereClause.length() > 0) {
            sql += whereClause.substring(3, whereClause.length());
            cntsql += whereClause.substring(3, whereClause.length());
        } else {
            sql = sql.replace("where", "");
            sql = sql.replace("WHERE", "");

            cntsql = cntsql.replace("where", "");
            cntsql = cntsql.replace("WHERE", "");
        }
        sql += orderBy;
        Long cnt = countBySql(cntsql, params);
        List<Map<String, Object>> list = null;
        if (cnt > 0) {
            if (pageSize != null && pageSize > 0 && pageNum != null && pageNum > 0) {
                list = findMapBySql(sql, params, pageNum, pageSize);
            } else {
                list = findMapBySql(sql, params);
            }
        }

        Map<String, Object> data = new HashMap<>();
        data.put("count", cnt);
        data.put("list", list);

        Result result = new Result();
        result.setData(data);
        return result;
    }

    @Override
    public Result getUserList(String roleId,String name, String select, Integer pageNum, Integer pageSize) throws Exception {
        String sql = "select u.id,u.version,u.mobile,u.name,u.login_name from tbl_user u left join tbl_user_role ur on ur.userId = u.id where roleId = :roleId ";
        String cntsql = "select count(*) from tbl_user u left join tbl_user_role ur on ur.userId = u.id where roleId = :roleId ";
        if ("0".equals(select)) {
            sql = "select u.id,u.version,u.mobile,u.name,u.login_name from tbl_user u  where id not in ( select userId from tbl_user_role where roleId = :roleId)";
            cntsql = "select count(*) from tbl_user u where id not in ( select userId from tbl_user_role where roleId = :roleId) ";
        }
        String orderBy = " order by  u.create_date_time desc ";
        StringBuilder whereClause = new StringBuilder();
        Map<String, Object> params = new HashMap<>();
        params.put("roleId", roleId);
        if (StringUtils.isNotEmpty(name)) {
            whereClause.append("and  (u.name like :name ) ").append(" ");
            params.put("name", "%"+name+"%");
        }
        if (whereClause.length() > 0) {
            sql += whereClause.toString();
            cntsql += whereClause.toString();
        }
        sql += orderBy;
        Long cnt = countBySql(cntsql, params);
        List<Map<String, Object>> list = null;
        if (cnt > 0) {
            if (pageSize != null && pageSize > 0 && pageNum != null && pageNum > 0) {
                list = findMapBySql(sql, params, pageNum, pageSize);
            } else {
                list = findMapBySql(sql, params);
            }
        }

        Map<String, Object> data = new HashMap<>();
        data.put("count", cnt);
        data.put("list", list);

        Result result = new Result();
        result.setData(data);
        return result;
    }
}
